package com.manager.plat.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IBalanceDao;
import com.manager.plat.dao.IConfigRouterDao;
import com.manager.plat.util.ExcelCreate;
import com.manager.plat.vo.BalanceVo;
import com.manager.plat.vo.TermVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class BalancePartnerAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8402810629373679678L;

	private static Logger logger = Logger.getLogger(BalancePartnerAction.class);
	
	private IBalanceDao balanceDao;						//结算信息数据接口
	private IConfigRouterDao configRouterDao;			//路由信息数据接口
	private List<BalanceVo> balanceVoList;				//结算信息VO
	private List<String> spnumberList;					//长号码信息列表
	private TermVo term;								//查询条件VO
	private String exportName;							//导出表格名称
	
	/**进入业务结算页面*/
	public String getRouter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		/**根据用户Id获得渠道下对应的长号码*/
		spnumberList = configRouterDao.getSpnumberListLowerByUserId(userInfoVo.getId());
		term = null;
		balanceVoList = null;
		
		return SUCCESS;
	}
	/**查询业务结算信息*/
	public String getRouterBalance(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		/**获得用户ID*/
		int userId = userInfoVo.getId();
		/**获得业务号码*/
		String sp_number = term.getSp_number();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		logger.info(String.format("渠道查询收入条件[sp_number=%s, time_begin=%s, time_end=%s]", sp_number, time_begin, time_end));
		
		balanceVoList = balanceDao.getRouterBalancePartner(userId, sp_number, time_begin, time_end);
		
		return SUCCESS;
	}
	
	public String exportRouterBalance(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		/**获得用户ID*/
		int userId = userInfoVo.getId();
		/**获得业务号码*/
		String sp_number = term.getSp_number();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		logger.info(String.format("渠道查询收入条件[sp_number=%s, time_begin=%s, time_end=%s]", sp_number, time_begin, time_end));
		
		balanceVoList = balanceDao.getRouterBalancePartner(userId, sp_number, time_begin, time_end);
		
		ExcelCreate excelCreate = new ExcelCreate();
		excelCreate.createRow(0);
		writeRouterTitle(excelCreate);
		writeRouterBill(excelCreate,balanceVoList);
		
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			excelCreate.downloadExcel(excelCreate.getWorkbook(), response,exportName);
		} catch (IOException e) {
			logger.debug("下载业务结算异常：" + ExceptionUtils.getFullStackTrace(e));
		}
		
		return null;
	}
	
	private void writeRouterTitle(ExcelCreate excelCreate){
		excelCreate.setCellTitle(0, "业务长号码");
		excelCreate.setCellTitle(1, "同步数量");
		excelCreate.setCellTitle(2, "计费类型");
		excelCreate.setCellTitle(3, "计费金额(元)");
		excelCreate.setCellTitle(4, "同步计费时长(分)");
		excelCreate.setCellTitle(5, "同步收入(元)");
	}
	private void writeRouterBill(ExcelCreate excelCreate,List<BalanceVo> balanceVoList){
		int currentRow = 1;
		if(balanceVoList != null && balanceVoList.size() > 0){
			int fee_minute_count = 0;
			int fee_price_count = 0;
			for(BalanceVo balanceVo : balanceVoList){
				excelCreate.createRow(currentRow);
				excelCreate.setCellTitle(0, balanceVo.getSp_code());
				
				excelCreate.setCellTitle(1, String.valueOf(balanceVo.getFee_user_num()));
				if(balanceVo.getFee_type() == 1){
					excelCreate.setCellTitle(2, "按时计费");
				}else if(balanceVo.getFee_type() == 2){
					excelCreate.setCellTitle(2, "按条计费");
				}
				
				excelCreate.setCellTitle(3, String.valueOf(balanceVo.getFee_code()));
				excelCreate.setCellTitle(4, String.valueOf(balanceVo.getFee_minute()));
				excelCreate.setCellTitle(5, String.valueOf(balanceVo.getFee_price()));
				
				fee_minute_count += balanceVo.getFee_minute();
				fee_price_count += balanceVo.getFee_price();
				currentRow++;
			}
			
			excelCreate.createRow(currentRow);
			excelCreate.setCellTitle(0, "总计");
			excelCreate.setCellTitle(1, "");
			excelCreate.setCellTitle(2, "");
			excelCreate.setCellTitle(3, "");
			excelCreate.setCellTitle(4, String.valueOf(fee_minute_count));
			excelCreate.setCellTitle(5, String.valueOf(fee_price_count));
		}
	}
	
	public IBalanceDao getBalanceDao() {
		return balanceDao;
	}
	public void setBalanceDao(IBalanceDao balanceDao) {
		this.balanceDao = balanceDao;
	}
	public IConfigRouterDao getConfigRouterDao() {
		return configRouterDao;
	}
	public void setConfigRouterDao(IConfigRouterDao configRouterDao) {
		this.configRouterDao = configRouterDao;
	}
	public List<String> getSpnumberList() {
		return spnumberList;
	}
	public void setSpnumberList(List<String> spnumberList) {
		this.spnumberList = spnumberList;
	}
	public TermVo getTerm() {
		return term;
	}
	public void setTerm(TermVo term) {
		this.term = term;
	}
	public String getExportName() {
		return exportName;
	}
	public void setExportName(String exportName) {
		this.exportName = exportName;
	}
	public List<BalanceVo> getBalanceVoList() {
		return balanceVoList;
	}
	public void setBalanceVoList(List<BalanceVo> balanceVoList) {
		this.balanceVoList = balanceVoList;
	}
}
